# load AIS data by month and create trajectories

# to make sure the 64bit version is used
# C:\Python27\ArcGISx6410.5\python.exe cleanAIS_1516.py

import arcpy
import os
import time
import calendar
import zipfile
import shutil
import mergeAIScsv_points as mergeAIS

prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],"Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj")
spatialRef = arcpy.SpatialReference(prjFile)

# allowing for files to be overwritten
arcpy.env.overwriteOutput=True

CORES = 4 # 8 

# locations and labels
data_dir = r"H:\My Drive\Boats\ReplicationCode\data\AIS"
arcpy.env.workspace = data_dir

# Location to Store Results
out_folder = r"H:\My Drive\Boats\ReplicationCode\data\AIS\lines_v2\%s"
out_gdb = "lines%02d.gdb" # output gdb

out_str = "westcoast5_lines_%s_%s" # year, month

tag = "Zone%s_%s_%02d" # (zone,year,month) name of folder to unzip

# folder location of input gdb
source_gdb_folder = r"%s\%02d_%s_%s"
source_gdb_tag = r"Zone%s_%s_%02d.gdb" 
source_gdb_str = os.path.join(source_gdb_folder,source_gdb_tag)

# folder location for 2015-2016
source_tag_1516=r"%s\AIS_%s_%02d_Zone%02d.zip" # year year month zone 
dest_tag_1516=source_gdb_folder # year month month year 
source_str_1516 = os.path.join(data_dir,source_gdb_folder,r"AIS_%s_%02d_Zone%02d.csv")

def main() :
    
    start = time.time()

    arcpy.env.workspace = data_dir

    # MONTHS
    months = range(1,13)

    # ZONES
    zones = [3,4,5,6,7,8,9,10,11] # includes AK and HI
	
    # YEARS 
    years = [2015,2016]

    #TESTING
    #months = [1,2]
    #zones = [3,4,5,6,7,8,9,10,11] # includes AK and HI
    #years = [2015]

    # create output folders and gdbs
    print "Creating output directories and gdbs"
    for year in years :
        folder = os.path.join( out_folder  % (year)  )
        # create folders if needed
        if not os.path.exists( folder ) :
            os.makedirs( folder )
            print "made directory: %s" % folder
 
        for month in months :
            gdb_path = os.path.join(out_folder % (year) , out_gdb % (month))
            if not arcpy.Exists(gdb_path) == True:
                print  "Creating %s" % gdb_path
                arcpy.CreateFileGDB_management( out_folder % (year) , out_gdb % (month) )
        
    # Extracting data and putting in format that can be used by track builder
    print "Unzipping"
    for y in years :
        for m in months :
            print y,m
            unzip_month_1516(m,zones,y)
        
    #Combine CSVs to points in single file
    "Merging CSVs"
    for y in years :
        for m in months :
            print y, m
            mergeAIS.join_AIS_CSV(zones,y,m,data_dir)
            print("--- %s seconds ---" % (time.time() - start))

    ### Then create folders using this code
    ### Then use bat file runTrackBuilder_1516
    ### NEED TO MAKE SURE TRACK CREATION PARAMETERS AND CODE ARE THE SAME IN
    ### TrackBuilder_M and in 64bit version
    ###################################################### 
     

#       
#
#    # create output folders and gdbs
#    print "Creating output directories and gdbs"
#    for year in years :
#        folder = os.path.join( out_folder  % (year)  )
#        # create folders if needed
#        if not os.path.exists( folder ) :
#            os.makedirs( folder )
#            print "made directory: %s" % folder
# 
#        for month in months :
#            gdb_path = os.path.join(out_folder % (year) , out_gdb % (month))
#            if not arcpy.Exists(gdb_path) == True:
#                print  "Creating %s" % gdb_path
#                arcpy.CreateFileGDB_management( out_folder % (year) , out_gdb % (month) )
#                                
#                #arcpy.Delete_management (gdb_path)
#                #print arcpy.GetMessages()
#                #print arcpy.GetMessages()
#

        

def unzip_month_1516(m,zones,year) :

        for zone in zones:
            file_name = os.path.join(data_dir, source_tag_1516 %( year, year , m , zone ) )
            folder =os.path.join(data_dir, source_gdb_folder %( year, m , calendar.month_name[m] , year ) )  # year month month year

            if not os.path.exists(folder):
                os.makedirs(folder)

            # just extracting file, not directories
            with zipfile.ZipFile(file_name) as zip_file:
                for member in zip_file.namelist():
                    filename = os.path.basename(member)
                    # skip directories
                    if not filename:
                        continue

                    # copy file (taken from zipfile's extract)
                    source = zip_file.open(member)
                    target = file(os.path.join(folder, filename), "wb")
                    with source, target:
                        shutil.copyfileobj(source, target)


if __name__ == '__main__':
    main()














